#include "getoption.h"

int HttpdGetOptions(int argc, char *const *argv)
{
    unsigned char     *p;
    int   i;

    for (i = 1; i < argc; i++) {
        p = (unsigned char *) argv[i];
        if (*p++ != '-') {
            log("invalid option: \"%s\"\n", argv[i]);
            return HTTP_ERROR;
        }

        while (*p) {

            switch (*p++) {

            case '?':
            case 'h':
                http_show_version = 1;
                http_show_help = 1;
                break;

            case 'v':
                http_show_version = 1;
                break;

            case 'V':
                http_show_version = 1;
                http_show_configure = 1;
                break;

            case 't':
                http_test_config = 1;
                break;

            case 'q':
                http_quiet_mode = 1;
                break;
                            
            case 'n':
                http_normal_mode = 1;
                break;    

            case 'p':
                if (*p) {
                    http_prefix = p;
                    goto next;
                }

                if (argv[++i]) {
                    http_prefix = (unsigned char *) argv[i];
                    goto next;
                }

                log("option \"-p\" requires directory name\n");
                return HTTP_ERROR;

            case 'c':
                if (*p) {
                    http_conf_file = p;
                    goto next;
                }

                if (argv[++i]) {
                    http_conf_file = (unsigned char *) argv[i];
                    goto next;
                }

                log("option \"-c\" requires file name\n");
                return HTTP_ERROR;

            case 'g':
                if (*p) {
                    http_conf_params = p;
                    goto next;
                }

                if (argv[++i]) {
                    http_conf_params = (unsigned char *) argv[i];
                    goto next;
                }

                log("option \"-g\" requires parameter\n");
                return HTTP_ERROR;   

            case 's':
                if (*p) {
                    http_signal = (char *) p;

                } else if (argv[++i]) {
                    http_signal = argv[i];

                } else {
                    log("option \"-s\" requires parameter\n");
                    return HTTP_ERROR;
                }

                if (strcmp(http_signal, "stop") == 0
                    || strcmp(http_signal, "quit") == 0
                    || strcmp(http_signal, "reopen") == 0
                    || strcmp(http_signal, "reload") == 0)
                {
                    //http_process = HTTP_PROCESS_SIGNALLER;
                    goto next;
                }

                log("invalid option: \"-s %s\"\n", http_signal);
                return HTTP_ERROR;

            default:
                log("invalid option: \"%c\"\n", *(p - 1));
                return HTTP_ERROR;
            }
        }

    next:

        continue;
    }

    return HTTP_TRUE;
}

void HttpdShowVersion() 
{
    if (http_show_version) {
        log("httpd version: " HTTP_VER "\n");

        if (http_show_help) {
            log("Usage: httpd [-?hvVtq] [-s signal] [-c filename] "
                             "[-p prefix] [-g directives]" CRLF CRLF
                "Options:" CRLF
                "  -?,-h         : this help" CRLF
                "  -v            : show version and exit" CRLF
                "  -V            : show version and configure options then exit"
                                   CRLF
                "  -t            : test configuration and exit" CRLF
                "  -q            : suppress non-error messages "
                                   "during configuration testing" CRLF
                "  -s signal     : send signal to a master process: "
                                   "stop, quit, reopen, reload" CRLF
                "  -c filename   : set configuration file (default: "
                                   HTTP_CONF_PATH ")" CRLF
                "  -g directives : set global directives out of configuration "
                                   "file" CRLF
                );
        }

        if (http_show_configure) {
            log("configure arguments\n");
        }

        if (!http_test_config) {
            exit(0);
        }
    }    
}
